package com.member.web.controller.shop;

import com.member.shop.domain.SysBizGradeInfo;
import com.member.shop.service.ISysBizUserIntegralRecordService;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizIntegralConfig;
import com.member.shop.service.ISysBizIntegralConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分配置Controller
 *
 * @author thomas
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/shop/other/integralConfig")
public class SysBizIntegralConfigController extends BaseController {
    @Resource
    private ISysBizIntegralConfigService sysBizIntegralConfigService;

    private void setTenantId(SysBizIntegralConfig sysBizIntegralConfig) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizIntegralConfig.setTenantId(tenantId);
        }
    }

    /**
     * 查询积分配置列表
     */
    @PreAuthorize("@ss.hasPermi('other:integralConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizIntegralConfig sysBizIntegralConfig) {
        startPage();
        List<SysBizIntegralConfig> list = sysBizIntegralConfigService.selectSysBizIntegralConfigList(sysBizIntegralConfig);
        return getDataTable(list);
    }

    /**
     * 导出积分配置列表
     */
    @PreAuthorize("@ss.hasPermi('other:integralConfig:export')")
    @Log(title = "积分配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizIntegralConfig sysBizIntegralConfig) {
        List<SysBizIntegralConfig> list = sysBizIntegralConfigService.selectSysBizIntegralConfigList(sysBizIntegralConfig);
        ExcelUtil<SysBizIntegralConfig> util = new ExcelUtil<SysBizIntegralConfig>(SysBizIntegralConfig.class);
        util.exportExcel(response, list, "积分配置数据");
    }

    /**
     * 获取积分配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('other:integralConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysBizIntegralConfigService.selectSysBizIntegralConfigById(id));
    }

    /**
     * 新增积分配置
     */
    @PreAuthorize("@ss.hasPermi('other:integralConfig:add')")
    @Log(title = "积分配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizIntegralConfig sysBizIntegralConfig) {
        SysBizIntegralConfig bizIntegralConfig = sysBizIntegralConfigService.selectSysBizIntegralConfigByTypeCode(sysBizIntegralConfig.getTypeCode(), sysBizIntegralConfig.getTenantId());
        if (bizIntegralConfig != null) {
            return AjaxResult.error("当前获取方式已配置，无法重复配置");
        }

        return toAjax(sysBizIntegralConfigService.insertSysBizIntegralConfig(sysBizIntegralConfig));
    }

    /**
     * 修改积分配置
     */
    @PreAuthorize("@ss.hasPermi('other:integralConfig:edit')")
    @Log(title = "积分配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizIntegralConfig sysBizIntegralConfig) {

        SysBizIntegralConfig bizIntegralConfig = sysBizIntegralConfigService.selectSysBizIntegralConfigByTypeCode(sysBizIntegralConfig.getTypeCode(), sysBizIntegralConfig.getTenantId());
        if (bizIntegralConfig != null && bizIntegralConfig.getId() != sysBizIntegralConfig.getId()) {
            return AjaxResult.error("当前获取方式已配置，无法重复配置");
        }

        return toAjax(sysBizIntegralConfigService.updateSysBizIntegralConfig(sysBizIntegralConfig));
    }

    @Autowired
    private ISysBizUserIntegralRecordService userIntegralRecordService;

    /**
     * 删除积分配置
     */
    @PreAuthorize("@ss.hasPermi('other:integralConfig:remove')")
    @Log(title = "积分配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

//        for (Long id : ids) {
//
//            SysBizIntegralConfig sysBizIntegralConfig = sysBizIntegralConfigService.selectSysBizIntegralConfigById(id);
//
//            if(sysBizIntegralConfig!=null){
//
//            }
//
//        }
        return toAjax(sysBizIntegralConfigService.deleteSysBizIntegralConfigByIds(ids));
    }
}
